package com.hdax.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hdax.entity.Permission;
import com.hdax.dao.PermissionMapper;
import com.hdax.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.vo.PermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-03-22
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {
    @Autowired
    private PermissionMapper dao;

    @Override
    public List<PermissionVo> selectAllPermission(Integer roleId) {
        List<PermissionVo> permissionVos = dao.selectAllPermisson();
        List<Integer> rids = dao.getPermissionId(roleId);
        System.out.println(rids+"==============================");
        for (PermissionVo permissionVo : permissionVos) {
            if(rids.contains(permissionVo.getId())){
                permissionVo.setChecked(true);
            }
        }
        return permissionVos;
    }

    @Override
    public void deleteByRid(Integer roleId) {
        dao.deleteByRid(roleId);
    }

    @Override
    public int addPermission(Integer roleId,List<Integer> permissions) {
        return dao.addPermission(roleId,permissions);
    }

    @Override
    public List<Permission> selectPermission() {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("disable","false");
        return dao.selectList(queryWrapper);
    }

    @Override
    public int addPermission(Permission permission) {
        return dao.insert(permission);
    }

    @Override
    public int updatePermission(Permission permission) {
        return dao.updateById(permission);
    }

    @Override
    public Permission selectById(Integer id) {
        return dao.selectById(id);
    }

    @Override
    public int selectCountById(Integer id) {
        return dao.selectCountById(id);
    }

    @Override
    public int deletePermission(Integer id) {
        return dao.deleteById(id);
    }

    @Override
    public List<Permission> findParentByRoleId(Integer roleId) {
        return dao.findParentByRoleId(roleId);
    }

    @Override
    public List<Permission> findChildrenByRoleId(Integer parent, Integer roleId) {
        return dao.findChildrenByRoleId(parent,roleId);
    }
}
